package org.csu.backofficemanagersys.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.csu.backofficemanagersys.domain.Admin;
import org.csu.backofficemanagersys.domain.AdminLog;
import org.csu.backofficemanagersys.domain.Category;
import org.csu.backofficemanagersys.service.AdminLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class LogInterceptor implements HandlerInterceptor {

    @Autowired
    private  AdminLogService adminLogService;


    AdminLog adminLog = new AdminLog();
    void insert(HttpServletRequest request ,String event){

        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String date1 = formatter.format(date);
        Admin admin =  new Admin();
        adminLog.setUsername("1");
        adminLog.setDate(date1);
        adminLog.setEvent(event);
        adminLogService.insertLog(adminLog);
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
        //默认值为false，代表不继续往后执行。如果为true，即使拦截了也依然会执行被拦截请求对应的controller层对应的方法。因此为true和没加拦截没区别
        //为false的话，被拦截的请求不会被执行它对应的controller方法
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        String uri = request.getRequestURI();
        switch (uri) {
            case "/login" -> {
                if ((int)request.getSession().getAttribute("isLogin") == 1) {
                    insert(request,"登录");
                }
            } //现在的问题是不清楚访问这个路径时登陆失败还是成功，可以尝试加入一个校验码,另一个问题是如何获得请求头，因为我的请求头一般都是对象而不是直接的参数
            case "/category" -> insert(request,"查看所有category");
            case "/category/update" -> {
                request.getParameter("catid");
                insert(request,"修改category");
            }
            case "/category/delete" ->{
                String catid = request.getParameter("catid");
                insert(request,"删除category:"+catid);
            }

            case "/category/insert" -> {
                String category = request.getParameter("catid");
//                String s = request.getHeader("category");
                insert(request,"新增category:"+category);}
            case "/product" -> insert(request,"查看所有product");
            case "/product/update" -> {
                String productid = request.getParameter("productid");
                insert(request,"修改product:"+productid);}
            case "/product/delete" -> {
                String productid = request.getParameter("productid");
                insert(request,"删除product:"+productid);}
            case "/product/insert" -> {
                String productid = request.getParameter("productid");
                insert(request,"新增product:"+productid);

            }
            case "/item" -> insert(request,"查看所有item");
            case "/item/update" -> {
                String itemid = request.getParameter("itemid");
                insert(request,"修改item:"+itemid);}
            case "/item/delete" -> {
                String itemid = request.getParameter("itemid");
                insert(request,"删除item:"+itemid);}
            case "/item/insert" -> {
                String itemid = request.getParameter("itemid");
                insert(request,"新增item:"+itemid);}
        }
    }

}
